﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.DataAccess;
using HTTT.Bussiness;
using System.Configuration;
using HTTT.AD;

namespace HTTT.admin.controls
{
    public partial class ucUserDetail : System.Web.UI.UserControl
    {
        private string _username;
        private HT_User _userInfo;
        private bool _isReadOnly;
        public string UserName
        {
            get { return _username; }
            set { _username = value; }
        }

        public HT_User UserInfo
        {
            get { return _userInfo; }
            set { _userInfo = value; }
        }

        public bool IsReadOnly
        {
            get { return _isReadOnly; }
            set { _isReadOnly = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string ouPath = LoadUserDetail();
                LoadOU(ouPath);
                SetReadOnly(IsReadOnly);
            }
        }

        private void SetReadOnly(bool IsReadOnly)
        {
            // Get all TextBoxes and set the value of the ReadOnly property.
            foreach (var tb in this.Controls.OfType<TextBox>())
                tb.ReadOnly = IsReadOnly;
            //ddlOU.Enabled = !IsReadOnly;
            chklAccountOptions.Enabled = !IsReadOnly;
        }

        private void LoadOU(string selectedValue)
        {
            //Get OU list
            HT_OUBO Bo = new HT_OUBO();
            List<HT_OU> ListBO = Bo.GetAllHT_OU().ToList();
            ddlOU.DataSource = ListBO;
            ddlOU.DataValueField = ADOUProperties.DISTINGUISHEDNAME;
            ddlOU.DataTextField = ADOUProperties.NAME;
            ddlOU.DataBind();
            ddlOU.Items.Insert(0, new ListItem("▼ " + ConfigurationManager.AppSettings["LDAPDomain"], ConfigurationManager.AppSettings["DomainPath"]));
            ddlOU.SelectedValue = selectedValue;
        }


        private string LoadUserDetail()
        {
            if (UserInfo == null)
            {
                //Get UserInfo by username
                HT_UserDAO dao = new HT_UserDAO();
                UserInfo = dao.GetHT_UserByUsername(UserName);
            }
            if (UserInfo != null)
            {
                txtgivenName.Text = UserInfo.givenName;
                txtmidleName.Text = UserInfo.initials;
                txtsurName.Text = UserInfo.sn;
                txtDisplayName.Text = UserInfo.displayName;
                txtDescription.Text = UserInfo.description;
                txtMail.Text = UserInfo.mail;
                txtHomePage.Text = UserInfo.wWWHomePage;
                txtUserName.Text = UserInfo.sAMAccountName;
                txtJob.Text = UserInfo.title;
                txtPhysicalDeliveryOfficeName.Text = UserInfo.physicalDeliveryOfficeName;
                txtCompany.Text = UserInfo.company;
                txtDepartment.Text = UserInfo.department;
                txtAddress1.Text = UserInfo.streetAddress;
                txtAddress2.Text = UserInfo.postOfficeBox;
                txtPostalcode.Text = UserInfo.postalCode;
                txtCity.Text = UserInfo.l;
                txtProvince.Text = UserInfo.st;
                txtCountry.Text = UserInfo.c;
                txtHomephone.Text = UserInfo.homephone;
                txtMobile.Text = UserInfo.mobile;
                txtOfficephone.Text = UserInfo.voiceTelephoneNumber;
                txtFax.Text = UserInfo.facsimileTelephoneNumber;
                txtIpphone.Text = UserInfo.ipPhone;
                //txtNote.Text = UserInfo.info;
                long AccountOptions = (long)UserInfo.userAccountControl;
                foreach (ListItem i in chklAccountOptions.Items)
                {
                    if ((AccountOptions & Convert.ToInt64(i.Value)) > 0)
                    {
                        i.Selected = true;
                    }
                }
                string ouPath = UserInfo.distinguishedName;
                ouPath = ouPath.Substring(ouPath.IndexOf(',') + 1);
                return ouPath;
            }
            return "";
        }
    }
}